﻿Public Class oDocProperty
    Public Function UpdateJsonDocProperty(ByVal propertyID As String, _
                                       ByVal propertyCode As String, ByVal propertyName As String, ByVal companyCode As String, _
                                       ByVal createdUserId As String, ByVal propertyValueType As String, ByVal databaseName As String, ByVal isInsert As Boolean) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(databaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {propertyCode, propertyName, companyCode, propertyValueType, createdUserId}
                Dim query As New StringBuilder
                If isInsert = True Then
                    query.Append(" INSERT INTO [oDocProperty]")
                    query.Append("  ([PropertyCode]")
                    query.Append(" ,[PropertyName]")
                    query.Append(" ,[CompanyCode]")
                    query.Append(" ,[PropertyValueType]")
                    query.Append(" ,[CreatedUserID])")
                    query.Append(" VALUES (@Param1,@Param2,@Param3,@Param4,@Param5)")
                    Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
                Else
                    query.Append("UPDATE  [oDocProperty] SET ")
                    query.Append("  [PropertyName] = @Param1")
                    query.Append("  ,[PropertyCode] = @Param2")
                    query.Append(" ,[PropertyValueType]= @Param3")
                    query.Append(" WHERE [PropertyID] = @Param4")
                    connect.Object_Execute_SAP(query.ToString(), New Object() {propertyName, propertyCode, propertyValueType, propertyID})
                End If
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function DeleteJsonDocProperty(ByVal dataBaseName As String, ByVal propertyID As String) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {propertyID}
                Dim query As New StringBuilder
                query.Append(" DELETE FROM [oDocProperty]")
                query.Append("  WHERE oDocProperty.PropertyID =  @Param1")
                query.Append(" DELETE FROM [oDocPropertyDetail]")
                query.Append("  WHERE oDocPropertyDetail.PropertyID =  @Param1")

                Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function GetAllDocProperty(ByVal dataBaseName As String, ByVal companyCode As String, ByVal referenceCode As String, ByVal isAdmin As Boolean) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oDocProperty")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                Dim query As New StringBuilder
                If isAdmin Then
                    query.Append("SELECT * FROM oDocProperty T0 JOIN oCompany T1 ON T0.CompanyCode = T1.CompanyCode")
                    query.Append(" JOIN oUser T2 ON T0.CreatedUserID = T2.UserID ")
                    query.Append(" JOIN oReferenceValue T3 ON T0.PropertyValueType = T3.ReferenceValue")
                    query.Append(" WHERE T3.ReferenceCode=@Param1")
                    query.Append(" ORDER BY T0.PropertyID DESC")
                    ds = connect.ObjectGetAll_Query_SAP(query.ToString(), New Object() {referenceCode})
                Else
                    query.Append("SELECT * FROM oDocProperty T0 JOIN oCompany T1 ON T0.CompanyCode = T1.CompanyCode")
                    query.Append(" JOIN oUser T2 ON T0.CreatedUserID = T2.UserID ")
                    query.Append(" JOIN oReferenceValue T3 ON T0.PropertyValueType = T3.ReferenceValue")
                    query.Append(" WHERE T0.CompanyCode = @Param1    AND  T3.ReferenceCode=@Param2 ")
                    query.Append(" ORDER BY T0.PropertyID DESC")
                    ds = connect.ObjectGetAll_Query_SAP(query.ToString(), New Object() {companyCode, referenceCode})
                End If
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function GetDocPropertyByCode(ByVal dataBaseName As String, ByVal propertyCode As String, ByVal referenceCode As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oDocProperty")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                Dim query As New StringBuilder
                query.Append("SELECT * FROM oDocProperty  T0 JOIN oCompany   T1 ON T0.CompanyCode = T1.CompanyCode  ")
                query.Append(" JOIN oUser T2 ON T0.CreatedUserID = T2.UserID ")
                query.Append(" JOIN oReferenceValue T3 ON T0.PropertyValueType = T3.ReferenceValue")
                query.Append(" WHERE PropertyCode = @Param1   AND  T3.ReferenceCode=@Param2")
                ds = connect.ObjectGetAll_Query_SAP(query.ToString(), New Object() {propertyCode, referenceCode})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function

End Class
